import Ajax from '@/axios'

export default {
    name: 'workspace',
    state() {
        return {
            order: null,
            pageSet: null,
            auditOrder: null,
            auditPageSet: null
        }
    },
    mutations: {
        // 业务接单
        initOrder(state, data) {
            state.order = data
        },
        pushOrder(state, data) {
            state.order.unshift(data)
        },
        updatedOrder(state, data) {
            const isArr
                = Object.prototype.toString.call(data) === '[object Array]'
            if (isArr) {
                data.forEach(obj => {
                    state.order.forEach(item => {
                        if (obj.id === item.id) {
                            return Object.assign(item, obj)
                        }
                    })
                })
            } else {
                state.order.find(item => {
                    if (item.id === data.id) {
                        return Object.assign(item, data)
                    }
                })
            }
        },
        delOrder(state, id) {
            let index = -1
            state.order.find((item, i) => {
                if (item.id === id) {
                    index = i
                    return item
                }
            })
            state.order.splice(index, 1)
        },

        // 业务单审核
        initPageSet(state, data) {
            state.pageSet = data
        },
        initAuditOrder(state, data) {
            state.auditOrder = data
        },
        updatedAuditOrder(state, data) {
            state.auditOrder.find(item => {
                if (item.id === data.id) {
                    return Object.assign(item, data)
                }
            })
        },
        initAuditPageSet(state, data) {
            state.auditPageSet = data
        }
    },
    getters: {},
    actions: {
        // 业务接单列表增删改查
        getOrder({ commit }, params) {
            Ajax.post('/business/businessList', params).then(data => {
                const { list, count, page, totalPage } = data.data.data
                commit('initOrder', list)
                commit('initPageSet', {
                    count: Number(count),
                    page: Number(page),
                    totalPage: Number(totalPage)
                })
            })
        },
        addOrder({ commit }, params) {
            Ajax.post('/business/post', params).then(data => {
                if (params.id) {
                    commit('updatedOrder', params)
                } else {
                    console.log(data.data.data[0], '新建数据')
                    commit(
                        'pushOrder',
                        Object.assign(
                            { is_mine: 1, status: '0', status_msg: '未审核' },
                            params,
                            data.data.data[0]
                        )
                    )
                }
            })
        },
        delOrder({ commit }, params) {
            Ajax.post('/business/del', params).then(data => {
                commit('delOrder', params.id)
            })
        },
        submitAudit({ commit }, params) {
            let param = null
            if (params.ids.indexOf(',') !== -1) {
                param = params.ids.split(',').map(id => {
                    return {
                        status: '1',
                        status_msg: '审核中',
                        id
                    }
                })
            } else {
                param = {
                    id: params.ids,
                    status_msg: '审核中',
                    status: '1'
                }
            }
            Ajax.post('/business/referCheck', params).then(data => {
                commit('updatedOrder', param)
            })
        },

        // 业务单审核列表增删改查
        getAuditOrder({ commit }, params) {
            Ajax.post('/business/applyList', {
                ...params
            }).then(data => {
                commit('initAuditOrder', data.data.data.list)
            })
        },
        auditOrder({ commit }, params) {
            const megMap = {
                '2': '审核成功',
                '3': '审核失败'
            }
            Ajax.post('/business/check', params).then(data => {
                commit('updatedAuditOrder', {
                    ...params,
                    status_msg: megMap[`${params.status}`]
                })
            })
        }
    }
}
